<?php
/*** 
 *
 *
 *
 **/

require_once '../db/db.php';
require_once modpath('util','perm');

function getUserByPass( $user, $pass ) {

  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();

    $user = $db->quote($user);
    $pass = $db->quote($pass);

    $res = $db->query("SELECT user.id, name, permgrp FROM user 
                         WHERE user.name = $user
                           AND user.pass = $pass");
    $data = $res->fetch(PDO::FETCH_ASSOC);
    $res = NULL;
    return $data;
    } catch ( PDOException $e ) {
      print "ERROR: " . $e->getMessage() . "<br />";
      die();
    }
};

//this was causing breakage(being redeclared, even with a require_once, by being an inner func)
function tableToTree($tree) {
    $right = array();
    $return = array();
    $retptr = &$return;
    
    foreach( $tree as $node ) {
      if( count($right) > 0 ) {
        // XXX later this should be 
        for($o = $i = count($right)-1; $right[$i]['rgtn'] < $node['rgtn']; --$i ) {
          array_pop($right);
        }
        if( $i < $o )
          // XXX THIS SHOULD LATER BE A SPECIAL TYPE PER PERM
          $retptr = true;
        $retptr = &$right[$i]['arrptr'];
      }

      $retptr[$node['name']] = array();
      $retptr = &$retptr[$node['name']];

      $right[] = array('rgtn'=>$node['rgtn'],'arrptr'=>&$retptr);
    }
    $retptr = true; // last value should always be a child node

    return $return;
}
function getUserGrpTree( $grpid ) {
  // might want to move this to another file

  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();

    $user = $db->quote($user);
    $pass = $db->quote($pass);

    $res = $db->query("SELECT DISTINCT(permtree.lftn), permtree.rgtn, permtree.name FROM permlist LEFT JOIN permtree ON permtree.lftn <= permlist.lftn AND permtree.rgtn >= permlist.rgtn WHERE permlist.grpid = ${grpid} ORDER BY permtree.lftn ASC;");
    $data = $res->fetchAll(PDO::FETCH_ASSOC);
    $res = NULL;
    
    $tree = false;
    if( $data ) {
      $tree = array();
      foreach( $data as $node ) {
        $tree[$node['parent']] ;
      }
    }

    return tableToTree($data);
    } catch ( PDOException $e ) {
      print "ERROR: " . $e->getMessage() . "<br />";
      die();
    }
};

function getUserById( $uid ) { 
  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();

    $uid = (int)intval( $uid );

    $user = $db->query("SELECT user.id AS id, user.name AS name, user.mail AS email, user.joinDate, (SELECT COUNT(post.id) FROM post WHERE user.id = post.uid) AS postCount FROM user WHERE user.id = ${uid};");
    $data = $user->fetch(PDO::FETCH_ASSOC);
    $user = NULL;
    return $data;
    } catch ( PDOException $e ) {
      print "ERROR: " . $e->getMessage() . "<br />";
      die();
    }
};
function getUserByName( $name ) { 
  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();

    $name = $db->quote($name);

    $user = $db->query("SELECT user.name AS name, user.mail AS email, user.joinDate, (SELECT COUNT(post.id) FROM post WHERE user.id = post.uid) AS postCount FROM user WHERE user.name = ${name};");
    $data = $user->fetch(PDO::FETCH_ASSOC);
    $user = NULL;
    return $data;
    } catch ( PDOException $e ) {
      print "ERROR: " . $e->getMessage() . "<br />";
      die();
    }
}; 

function getUsers( $start, $inc ) { 
  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();

    $start = (int)intval( $start );
    $inc = (int)intval( $inc );

    $user = $db->query("SELECT user.name AS name, user.mail AS email, user.joinDate, (SELECT COUNT(post.id) FROM post WHERE user.id = post.uid) AS postCount FROM user WHERE user.id = ${uid};");
    $data = $res->fetch(PDO::FETCH_ASSOC);
    $res = NULL;
    return $data;
    return $res;
    } catch ( PDOException $e ) {
      print "ERROR: " . $e->getMessage() . "<br />";
      die();
    }
};

?>
